package com.mstore.action;

import java.util.List;
import java.util.Map;

import com.mstore.pojo.Account;
import com.mstore.pojo.Question;
import com.mstore.pojo.Reply;
import com.mstore.service.QuestionService;
import com.mstore.service.ReplyService;
import com.opensymphony.xwork2.ActionContext;

/**
 * 问题回复
 */
public class QuestionAction extends BaseAction {

	private static final long serialVersionUID = -4933767527910707175L;

	private String result;

	private Map<String, Object> session = ActionContext.getContext()
			.getSession();

	private QuestionService questionService;

	private ReplyService replyService;

	private List<Question> questions;

	private List<Reply> replies;

	private Question question;

	private Reply reply;

	private Integer questionType;

	private Long eaId;

	/**
	 * 查询问题
	 * 
	 * @return
	 */
	public String watchQuestions() {
		Object object = session.get("account");
		if (object == null) {
			result = ERROR_TIP;
			return "error";
		}
		Account account = (Account) object;
		question = new Question();
		question.setEaId(account.getId());

		questions = questionService.watchQuestions(question);

		for (Question question : questions) {
			reply = new Reply();
			reply.setId(question.getRelyId());
			reply = replyService.getOneReply(reply);
			question.setReply(reply);
		}

		return "toWatchQuestions";
	}

	/**
	 * 跳转到添加问题页面
	 * 
	 * @return
	 */
	public String initAddQuestion() {
		Object object = session.get("account");
		if (object == null) {
			result = ERROR_TIP;
			return "error";
		}
		Account account = (Account) object;
		reply = new Reply();
		reply.setEaId(account.getId());
		reply.setType(1);
		replies = replyService.watchReplyByType(reply);

		eaId = account.getId();

		return "toAddQuestion";
	}

	/**
	 * 保存问题
	 * 
	 * @return
	 */
	public String addQuestion() {
		Object object = session.get("account");
		if (object == null) {
			result = ERROR_TIP;
			return "error";
		}

		if (question.getContent() == null || "".equals(question.getContent())) {
			result = "关键字不能为空";
			return "error";
		}

		if (question.getRelyId() == null || question.getRelyId() == 0) {
			result = "回复内容不能为空";
			return "error";
		}

		Account account = (Account) object;
		question.setEaId(account.getId());
		question.setCreateTime(getCreateTime());
		questionService.saveQuestion(question);

		return "addSuccess";
	}

	/**
	 * 跳转到修改问题页面
	 * 
	 * @return
	 */
	public String initModifyQuestion() {
		question = questionService.getOneQuestion(question);

		Object object = session.get("account");
		if (object == null) {
			result = ERROR_TIP;
			return "error";
		}
		Account account = (Account) object;
		reply = new Reply();
		reply.setEaId(account.getId());
		reply.setType(question.getType());
		replies = replyService.watchReplyByType(reply);

		eaId = account.getId();

		return "toModifyQuestion";
	}

	/**
	 * 修改问题
	 * 
	 * @return
	 */
	public String modifyQuestion() {
		if (question.getContent() == null || "".equals(question.getContent())) {
			result = "关键字不能为空";
			return "error";
		}

		if (question.getRelyId() == null || question.getRelyId() == 0) {
			result = "回复内容不能为空";
			return "error";
		}
		questionService.modifyQuestion(question);
		return "modifySuccess";
	}

	/**
	 * 删除问题
	 * 
	 * @return
	 */
	public String delQuestion() {
		questionService.delQuestion(question);
		return "delSuccess";
	}

	/**
	 * 根据问题类型改变回复选项
	 * 
	 * @return
	 */
	public String changeReplyByType() {
		reply = new Reply();
		reply.setEaId(eaId);
		reply.setType(questionType);
		replies = replyService.watchReplyByType(reply);

		result = listTojson(replies);

		return "returnToChange";
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public List<Question> getQuestions() {
		return questions;
	}

	public void setQuestions(List<Question> questions) {
		this.questions = questions;
	}

	public Question getQuestion() {
		return question;
	}

	public void setQuestion(Question question) {
		this.question = question;
	}

	public QuestionService getQuestionService() {
		return questionService;
	}

	public void setQuestionService(QuestionService questionService) {
		this.questionService = questionService;
	}

	public ReplyService getReplyService() {
		return replyService;
	}

	public void setReplyService(ReplyService replyService) {
		this.replyService = replyService;
	}

	public List<Reply> getReplies() {
		return replies;
	}

	public void setReplies(List<Reply> replies) {
		this.replies = replies;
	}

	public Reply getReply() {
		return reply;
	}

	public void setReply(Reply reply) {
		this.reply = reply;
	}

	public Integer getQuestionType() {
		return questionType;
	}

	public void setQuestionType(Integer questionType) {
		this.questionType = questionType;
	}

	public Long getEaId() {
		return eaId;
	}

	public void setEaId(Long eaId) {
		this.eaId = eaId;
	}

}
